<?php

namespace App\Http\Controllers\Front;

use App\Models\AgricGoods;
use App\Models\VillageThings;
use App\Models\VillageThingsComment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VillageThingsController extends Controller
{
    private $data = array();

    public function __construct(Request $request)
    {
        $this->data['pageTitle'] = '乡通-大事小情';
    }


    /**
     * 大事小情列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $this->data['hotGoods'] = AgricGoods::pc_hot_goods_list(); // 农品热销
        $this->data['positionInfo'] = ['name' => '大事小情', 'url' => ''];
        return view('front.village_things.index', $this->data);
    }

    /**
     * ajax获得大事小情列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ajax_things_lists(Request $request)
    {
        $pagesize  = $request->pagesize ?? 10;
        $thingsData = VillageThings::where('is_show', 1)
            ->with(['villager', 'region_info'])
            ->orderBy('is_top', 'desc')
            ->orderBy('weight', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($pagesize);
        $this->data['thingsData'] = $thingsData->toArray();
        return view('front.village_things.ajax_things_lists', $this->data);
    }

    /**
     * ajax获得大事小情评论列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ajax_things_common_lists(Request $request)
    {
        $pagesize  = $request->pagesize ?? 10;
        $thingsId  = $request->things_id;

        $thingsCommon = VillageThingsComment::where('village_things_id', $thingsId)
            ->where('is_show', 1)
            ->with(['replyer', 'commenter'])
            ->orderBy('created_at', 'asc')
            ->paginate($pagesize);

        $this->data['thingsCommon'] = $thingsCommon->toArray();
        $this->data['thingsId'] = $thingsId;
        return view('front.village_things.ajax_things_common_lists', $this->data);
    }

    /**
     * 大事小情详情页
     * @param Request $request
     * @param int $thingsId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function details(Request $request, $thingsId = 0)
    {
        $thingsData = VillageThings::where('is_show', 1)
            ->with(['villager', 'region_info'])
            ->find($thingsId);

        if (empty($thingsData)) {
            return redirect()->back();
        }

        $this->data['thingsId'] = $thingsId;
        $this->data['thingsData'] = $thingsData->toArray();
        return view('front.village_things.details', $this->data);
    }

}
